/*
 * Image.cpp
 *
 *  Created on: 23.02.2011
 *      Author: Markus
 */

#include "BadaImage.h"
#include "OpennavUtils.h"

BadaImage::BadaImage() :  __pCanvas(null), __width(0), __height(0), __pFont(null) {
}

result BadaImage::Construct(Osp::Graphics::Canvas* canvas) {
	__width = canvas->GetBounds().width;
	__height = canvas->GetBounds().height;
	__pCanvas = canvas;
	return E_SUCCESS;
}

BadaImage::~BadaImage() {
	// TODO: destruct
}

Osp::Graphics::Canvas* BadaImage::GetCanvas() {
	return __pCanvas;
}

void BadaImage::set_foreground_color(opennav::Color& color) {
	OpennavUtils::Convert(color, __foreground);
	__pCanvas->SetForegroundColor(__foreground);
}

void BadaImage::set_background_color(opennav::Color& color) {
	//Osp::Graphics::Color convertedColor;
	OpennavUtils::Convert(color, __background);
	__pCanvas->SetForegroundColor(__background);
}

void BadaImage::set_line_width(int32_t width) {
	__pCanvas->SetLineWidth(width);
}

void BadaImage::set_font(const opennav::Font& font) {
	if (__pFont != null) delete __pFont;
	__pFont = new Osp::Graphics::Font();
	OpennavUtils::Convert(font, *__pFont);
	__pCanvas->SetFont(*__pFont);
}

void BadaImage::draw_lines(const std::vector<opennav::Point>& polyline) {
	Osp::Base::Collection::IList* points = OpennavUtils::Convert(polyline);
	__pCanvas->DrawPolyline(*points);
	points->RemoveAll(true);
	delete points;
}

void BadaImage::draw_line(const opennav::Point& a, const opennav::Point& b) {
	Osp::Graphics::Point p1, p2;
	OpennavUtils::Convert(a, p1);
	OpennavUtils::Convert(b, p2);
	__pCanvas->DrawLine(p1, p2);
}


void BadaImage::draw_polygon(const std::vector<opennav::Point>& polyline) {
	Osp::Base::Collection::IList* points = OpennavUtils::Convert(polyline);
	__pCanvas->FillPolygon(__background, *points);
	points->RemoveAll(true);
	delete points;
}

void BadaImage::draw_rectangle(const opennav::Point& point, int32_t width, int32_t height) {
	Osp::Graphics::Point* converted = new Osp::Graphics::Point();
	OpennavUtils::Convert(point, *converted);
	Osp::Graphics::Rectangle rectangle(converted->x, converted->y, width, height);
	__pCanvas->DrawRectangle(rectangle);
	__pCanvas->FillRectangle(__background, rectangle);
	//__canvas.FillRectangle(Osp::Graphics::Color::COLOR_WHITE, rectangle);
	delete converted;
}

void BadaImage::draw_circle(const opennav::Point& center, int32_t radius) {
	__pCanvas->DrawEllipse(Osp::Graphics::Rectangle(center.x - radius, center.y - radius, radius * 2, radius * 2));
}

void BadaImage::draw_text(const opennav::Point& point, const std::string& text) {
	Osp::Base::String string;
	OpennavUtils::Convert(text, string);
	Osp::Graphics::Point p;
	OpennavUtils::Convert(point, p);
	__pCanvas->DrawText(p, string);
}

void BadaImage::draw_text_tilted(const opennav::Point& center, const std::string& text, int dx, int dy) {
	Osp::Base::String string;
	OpennavUtils::Convert(text, string);
	Osp::Graphics::Point p;
	OpennavUtils::Convert(center, p);
	Osp::Graphics::Dimension dimension;
	Osp::Graphics::Font* font = GetFont();
	font->GetTextExtent(string, string.GetLength(), dimension);
	Osp::Graphics::Rectangle bounds = Osp::Graphics::Rectangle(0,0,dimension.width, dimension.height);
	Osp::Graphics::Canvas* pTempCanvas = new Osp::Graphics::Canvas();
	pTempCanvas->Construct(bounds);
	pTempCanvas->SetForegroundColor(__foreground);
	pTempCanvas->SetFont(*font);
	pTempCanvas->DrawText(Osp::Graphics::Point(0, 0), string);
	Osp::Graphics::Bitmap* pBitmap = new Osp::Graphics::Bitmap();
	pBitmap->Construct(*pTempCanvas, bounds);
	int degree;
	if (dx == 0) {
		if (dy == 1)
			degree = -90;
		else
			degree = 90;
	} else {
		degree = -atanf((float)dy / (float)dx) * 180.0f / M_PI;
	}

	//p.x -= dimension.width / 2;
	//p.y -= dimension.height / 2;

	__pCanvas->DrawBitmap(p, *pBitmap, Osp::Graphics::Point(dimension.width / 2, dimension.height / 2), degree);
	delete pTempCanvas;
	delete pBitmap;

//	Osp::Base::String* string = OpennavUtils::Convert(text);
//	Osp::Graphics::Point p;
//	OpennavUtils::Convert(center, p);
//	Osp::Graphics::Dimension dimension;
//	Osp::Graphics::Font* font = GetFont();
//	font->GetTextExtent(*string, (*string).GetLength(), dimension);
//	Osp::Graphics::Rectangle bounds = Osp::Graphics::Rectangle(0,0,dimension.width, dimension.height);
//	Osp::Graphics::Canvas* pTempCanvas = new Osp::Graphics::Canvas();
//	pTempCanvas->Construct(bounds);
//	pTempCanvas->SetForegroundColor(__foreground);
//	pTempCanvas->SetFont(*font);
//	pTempCanvas->DrawText(Osp::Graphics::Point(0, 0), *string);
//	Osp::Graphics::Bitmap* pBitmap = new Osp::Graphics::Bitmap();
//	pBitmap->Construct(*pTempCanvas, bounds);
//	int degree;
//	if (dx == 0) {
//		if (dy == 1)
//			degree = -90;
//		else
//			degree = 90;
//	} else {
//		degree = -atanf((float)dy / (float)dx) * 180.0f / M_PI;
//	}
//	__pCanvas->DrawBitmap(p, *pBitmap, Osp::Graphics::Point(0, 0), degree);
//	delete pTempCanvas;
//	delete pBitmap;
//	delete string;
}


void BadaImage::get_text_dimension(const std::string& text, opennav::Dimension& dimension) {
	Osp::Base::String string;
	OpennavUtils::Convert(text, string);
	Osp::Graphics::Dimension dim;
	GetFont()->GetTextExtent(string, string.GetLength(), dim);
	OpennavUtils::Convert(dim, dimension);
}

int32_t BadaImage::width() {
	return __width;
}

int32_t BadaImage::height() {
	return __height;
}

Osp::Graphics::Font* BadaImage::GetFont() {
	if (__pFont == null) {
		__pFont = __pCanvas->GetFontN();
	}
	return __pFont;
}

